#include <gnome.h>
#include <stdio.h>
#include <ctype.h>

#include "support.h"

#include "interface.h"
#include "callbacks.h"

#include "preamble.h"

gchar *szFontName = NULL;

void change_title(void)		/* change the title of gCASL when new or load a program */
{
  gchar *title;

  if (statusSaved)
    if (filename)
      title=g_strdup_printf("%s - %s",_("gCASL"),filename); 
    else
      title=g_strdup_printf("%s",_("gCASL")); 
  else
    if (filename)
      title=g_strdup_printf("%s - %s (modified)",_("gCASL"),filename); 
    else
      title=g_strdup_printf("%s (modified)",_("gCASL")); 

  gtk_window_set_title (GTK_WINDOW (appMain),title);

  g_free(title);
}

void display(char *format, ...)
{
  #define USE_G_STRDUP_PRINTF 0

  #if USE_G_STRDUP_PRINTF
  gchar *buffer=NULL;
  #else
  gint  buffer_size=1024;
  gchar buffer[buffer_size];
  #endif
  va_list argptr;

  if (appMain)
    {
      va_start (argptr, format);
      #if USE_G_STRDUP_PRINTF
      buffer=g_strdup_printf(format,argptr);
      #else
      vsnprintf(buffer,buffer_size,format,argptr);
      #endif
      va_end (argptr);
  
      gtk_text_insert (GTK_TEXT (lookup_widget(GTK_WIDGET(appMain),"textScreen")), NULL, NULL, NULL, buffer, strlen(buffer));

      #if USE_G_STRDUP_PRINTF
      g_free(buffer);
      #endif
    }
  #undef USE_G_STRDUP_PRINTF
}

void display_with_length(char *string,int length)
{
  gtk_text_insert (GTK_TEXT (lookup_widget(GTK_WIDGET(appMain),"textScreen")), NULL, NULL, NULL, string, length);
}

/*
 * GetFont
 *
 * Show the font selection dialog to allow user to
 * pick a font.  Return a gchar * with the font
 * name.
 */
gchar *GetFont ()
{
  szFontName = NULL;
  
  /* --- Create the font selection dialog --- */
  /* --- Show the dialog --- */
  gtk_widget_show(create_fontselectiondialog());

  /* --- Modal - wait until destroyed --- */
  gtk_main();

  /* --- Return the font name --- */
  return (szFontName);
}

/*
 * SetStyleRecursively
 *
 * Set the style on the current widget and on any 
 * children widgets.
 */
void SetStyleRecursively (GtkWidget *widget, gpointer data)
{
    GtkStyle *style;

    /* --- Get the style --- */
    style = (GtkStyle *) data;

    /* --- Set style on current widget --- */
    gtk_widget_set_style (widget, style);

    /* --- If it has other widgets --- */
    if (GTK_IS_CONTAINER (widget)) {

        /* --- Set the style on those widgets too --- */
        gtk_container_foreach (GTK_CONTAINER (widget), 
                           SetStyleRecursively, style);
    }
}

void set_style(void)
{
  GtkStyle *style;
  GtkStyle *defstyle;

  defstyle = gtk_widget_get_default_style ();
  style = gtk_style_copy (defstyle);
  style->font = gdk_font_load("-sony-fixed-medium-r-*-*-*-*-*-*-*-*-*");
  SetStyleRecursively (lookup_widget(appMain,"clistMemory"), (gpointer) style);
  SetStyleRecursively (lookup_widget(appMain,"clistProgram"), (gpointer) style);
  SetStyleRecursively (lookup_widget(appMain,"clistStack"), (gpointer) style);
  SetStyleRecursively (lookup_widget(appMain,"clistVariables"), (gpointer) style);
}
